package cn.cai.service.impl;

import cn.cai.listener.SubjectReadListener;
import cn.cai.pojo.EduSubject;
import cn.cai.mapper.EduSubjectMapper;
import cn.cai.pojo.excell.SubjectExcelData;
import cn.cai.pojo.vo.SubjectOneData;
import cn.cai.pojo.vo.SubjectTwoData;
import cn.cai.service.IEduSubjectService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author caishenao
 * @since 2021-08-16
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements IEduSubjectService {

    @Autowired
    private IEduSubjectService subjectService;
    /**
     * 实现保存excel中的课程名
     * @param file 上传的文件
     */
    @Override
    public void addSubject(MultipartFile file) {
        InputStream inputStream = null;
        try {
            inputStream =  file.getInputStream();
            //使用EasyExcel以数据流的形式进行读操作
            //--TODO
            //尝试使用异步的形式，将表中的数据全部读出（List集合）
            //将List中的第一个值按map中的key存储
            //将List中的第二个值按map中的value存储
            //然后遍历map，将，map中的数据存进数据库中
            EasyExcel.read(inputStream, SubjectExcelData.class,new SubjectReadListener(subjectService))
                    .excelType(ExcelTypeEnum.XLSX)
                    .sheet()
                    .doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 实现树形结构查询课程信息的处理
     * @return 封装好的树形结构
     */
    @Override
    public List<SubjectOneData> getListTree() {
        //创建要查询的一级分类集合
        List<SubjectOneData> oneTree = new ArrayList<>();
        //查询所有的一级分类
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",0);
        List<EduSubject> subjectOneList = subjectService.list(queryWrapper);
        //查询所有二级分类
        queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("parent_id",0);
        List<EduSubject> subjectTwoList = subjectService.list(queryWrapper);
        for (EduSubject eduSubject:subjectOneList){
            SubjectOneData subjectOneData = new SubjectOneData();
            //将查询到的EduSubject类的属性复制到SubjectOneData中
            subjectOneData.setId(eduSubject.getId());
            subjectOneData.setTitle(eduSubject.getTitle());
            //对查询到的二级分类进行封装
            List<SubjectTwoData> twoTree = new ArrayList<>();
            for (int i = 0;i<subjectTwoList.size();i++){

                if (subjectOneData.getId().equals(subjectTwoList.get(i).getParentId())){
                    //找到了改一级分类下的一个二级分类
                    //对查询的到数据进行封装
                    SubjectTwoData subjectTwoData = new SubjectTwoData();
                    subjectTwoData.setId(subjectTwoList.get(i).getId());
                    subjectTwoData.setTitle(subjectTwoList.get(i).getTitle());
                    //将封装好的对象添加进二级分类的集合中
                    twoTree.add(subjectTwoData);
                }
            }
            //将二级分类集合，添加到他的一级分类中
            subjectOneData.setChildren(twoTree);
            oneTree.add(subjectOneData);
        }
        return oneTree;
    }
}
